package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysDepartDao;
import ysq.xyg.sys.model.po.SysDepart;
import ysq.xyg.sys.model.po.SysDepart;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysDepartVo;
import ysq.xyg.sys.service.SysDepartService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 系统管理-部门service实现
 *
 * @author shengQiang yu
 * @date 2019-4-2 15:43:10
 */
@Service("sysDepartService")
public class SysDepartServiceImpl extends BaseServiceImpl<SysDepart> implements SysDepartService {

    @Resource
    private SysDepartDao sysDepartDao;

    private SysDepart sysDepart;

    @Autowired
    public void setBaseDao(SysDepartDao sysDepartDao) {
        this.baseDao = sysDepartDao;
    }


    /**
     * 分页查询
     *
     * @param sysDepartVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public ReturnEntity page(SysDepartVo sysDepartVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysDepart> list = acceptList(sysDepartVo);
        PageInfo<SysDepart> page=new PageInfo<SysDepart>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysDepartVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public ReturnEntity addOrUpdate(SysDepartVo sysDepartVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysDepartVo.getId())){
            sysDepart=new SysDepart(currentUser);
            /**
             * 获取菜单的级别和code
             */
            if (StrUtil.isEmpty(sysDepartVo.getParentId())) {
                sysDepart.setGrade(BaseConstant.SYS_MENU_GRADE_ONE);
                //第一级
                SysDepart bm = new SysDepart();
                bm.setGrade(sysDepart.getGrade());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String menuCode = sysDepartDao.AcceptMaxDepartCode(bm);
                if(StringUtil.isEmpty(menuCode)){
                    sysDepart.setCode(BaseConstant.MENU_CODE_MIN);
                }else{
                    sysDepart.setCode(Long.parseLong(menuCode)+1+"");
                }
            } else {
                SysDepart parentMeun = sysDepartDao.selectByPrimaryKey(sysDepartVo.getParentId());
                sysDepart.setGrade((Integer.parseInt(parentMeun.getGrade())+1)+"");
                SysDepart bm = new SysDepart();
                bm.setParentId(sysDepartVo.getParentId());
                bm.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
                String menuCode = sysDepartDao.AcceptMaxDepartCode(bm);
                if(StringUtil.isEmpty(menuCode)){
                    sysDepart.setCode(parentMeun.getCode()+ BaseConstant.MENU_CODE_MIN);
                }else{
                    sysDepart.setCode(Long.parseLong(menuCode)+1+"");
                }
            }
            isUpate=false;
        }else{
            sysDepart=selectByPrimaryKey(sysDepartVo.getId());
            if(ObjectUtil.isNull(sysDepart)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysDepart.setUpdateTime(new Date());
            sysDepart.setUpdateUser(currentUser.getId());
        }
        sysDepart.setName(sysDepartVo.getName());
        sysDepart.setParentId(sysDepartVo.getParentId());
        sysDepart.setSort(sysDepartVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysDepart);
        }else{
            num=insert(sysDepart);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysDepart sysDepart=selectByPrimaryKey(id);
        if(sysDepart!=null){
            sysDepart.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            sysDepart.setUpdateTime(new Date());
            sysDepart.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(sysDepart);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param sysDepartVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public List<SysDepart> acceptList(SysDepartVo sysDepartVo){
        Example example = new Example(SysDepart.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysDepartVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysDepartVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(sysDepartVo.getName())){
            criteria.andLike("name","%"+sysDepartVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc");
        return sysDepartDao.selectByExample(example);
    }

    /**
     * 获取组织机构树
     *
     * @param sysDepartVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-2 15:43:10
     */
    public ReturnEntity dTreeList(SysDepartVo sysDepartVo) {
        List<SysDepart> list = acceptList(sysDepartVo);
        List<Map<String,Object>> dtreeEntityList=new ArrayList<Map<String,Object>>();
        for(SysDepart baseOrganization:list){
            Map<String,Object> dtreeEntity=new HashedMap();
            dtreeEntity.put("id",baseOrganization.getId());
            dtreeEntity.put("title",baseOrganization.getName());
            dtreeEntity.put("parentId", StrUtil.isEmpty(baseOrganization.getParentId())? "0":baseOrganization.getParentId());
            dtreeEntity.put("isLast",isLast(list,baseOrganization.getId()));
            Map<String,String> m=new HashedMap();
            m.put("type","0");
            m.put("isChecked","0");
            List<Map<String,String>> dtreeCheckEntityList=new ArrayList<Map<String,String>>();
            dtreeCheckEntityList.add(m);
            dtreeEntity.put("checkArr",dtreeCheckEntityList);
            dtreeEntityList.add(dtreeEntity);
        }
        return new ReturnEntity(dtreeEntityList);
    }

    /**
     *@Author: YuShengQiang
     *@Date: 2018-12-05 17:44
     *@Description:判断当时对象是否有子对象
     */
    private boolean isLast(List<SysDepart> list, String id){
        if(list!=null&&list.size()>0&&StringUtil.isNotEmpty(id)){
            for(SysDepart baseOrganization:list){
                if(id.equals(baseOrganization.getParentId())){
                    return false;
                }
            }
        }
        return true;
    }

}
